package com.example.administrator.ygddriver.tool;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.HashMap;

/**
 * Created by Administrator on 2016/11/17.
 */
public class OkGoRequests {

    public static final int SUCCESS = 1;
    public static final int ERROR = -1;
    private Gson gson;
    private static OkGoRequests okRequests;

    private OkGoRequests() {
        gson = new Gson();
    }

    public static OkGoRequests getInstance() {
        if (okRequests == null) {
            synchronized (OkGoRequests.class) {
                if (okRequests == null) {
                    okRequests = new OkGoRequests();
                }
            }
        }
        return okRequests;
    }

    //post
    public <T> void putRequestAsync(String url, HashMap hashMap, final Class<T> clazz, final OnTrue<T> onTrue, final OnError<T> onError) {
        final Handler handler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case SUCCESS:
                        onTrue.hasData((T) msg.obj);
                        break;
                    case ERROR:
                        onError.noData();
                        break;
                }
            }
        };
        try {
            OkGo.<String>post(UrlValuse.DOMAIN_NAME + url).tag(this).params(hashMap).execute(new StringCallback() {
                @Override
                public void onSuccess(Response<String> response) {
                    T t = null;
                    t = gson.fromJson(response.body(), clazz);
                    Message message = handler.obtainMessage();
                    message.what = SUCCESS;
                    message.obj = t;
                    handler.sendMessage(message);
                }

                @Override
                public void onError(Response<String> response) {
                    super.onError(response);
                    onError.noData();
                }
            });
        } catch (Exception e) {
            Toast.makeText(MyApp.context, "ip错误", Toast.LENGTH_SHORT).show();
        }

    }

//    public void postAcquireAsync(String url, HashMap hashMap) {
//        OkGo.<Json>post(url).tag(this).params(hashMap).execute(new StringCallback() {
//            @Override
//            public void onSuccess(Response<String> response) {
//                response.body();
//            }
//        });
//    }

//    //get
//    public <T> void getRequestAsync(String url, final Class<T> clazz, final OnTrue<T> onTrue, final OnError<T> onError) {
//        final Handler handler = new Handler(Looper.getMainLooper()) {
//
//
//            @Override
//            public void handleMessage(Message msg) {
//                super.handleMessage(msg);
//                switch (msg.what) {
//                    case SUCCESS:
//                        onTrue.hasData((T) msg.obj);
//                        break;
//                    case ERROR:
//                        onError.noData();
//                        break;
//                }
//
//
//            }
//        };
//        OkGo.get(url).tag(this).execute(new StringCallback() {
//            @Override
//            public void onSuccess(String s, Call call, Response response) {
//                T t = null;
//                t = gson.fromJson(s, clazz);
//                Message message = handler.obtainMessage();
//                message.what = SUCCESS;
//                message.obj = t;
//                handler.sendMessage(message);
//
//            }
//
//            @Override
//            public void onError(Call call, Response response, Exception e) {
//                super.onError(call, response, e);
//                onError.noData();
//            }
//
//            @Override
//            public void parseError(Call call, Exception e) {
//                super.parseError(call, e);
//                onError.noData();
//            }
//
//        });
//    }

    //成功
    public interface OnTrue<T> {
        void hasData(T date);
    }

    //失败
    public interface OnError<T> {
        void noData();
    }
}
